//
//  WebViewViewController.m
//  Init
//
//  Created by zhaoshijie on 20/01/2018.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "EBMWebViewViewController.h"
#import "SJDataManageTool.h"



@interface EBMWebViewViewController ()

@end

@implementation EBMWebViewViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //  创建控件    webView
    [self createViewInWebView];
    
}


//  创建控件    webView
- (void)createViewInWebView
{
    
    //  配置交互
    [self.userContent addScriptMessageHandler:self name:@"getLoginUser"];
    [self.userContent addScriptMessageHandler:self name:@"callMPShare"];
    [self.userContent addScriptMessageHandler:self name:@"userLogin"];
    

}




#pragma mark ----------初始化网络请求----------
//  初始化网络请求
- (void)requestData
{
    
}

//  网络请求    用户信息
- (void)requestDataInUserInfoWithSuccessBlock:(void(^)(id result))successBlock
{
    NSString *urlString = @"";
        

    [EBMRequest GETRequestTakeDataWithUrlString:urlString headerDic:nil parameters:nil loadView:self.view tableView:nil successBlock:^(id result) {
        
        
        
        if(successBlock)
        {
            successBlock(result);
        }
        
    } serverErrorBlock:nil failBlock:nil];
    
}


#pragma mark --------------方法--------------

//  加载网页
- (void)loadWebView
{
    
    //  设置网页的请求头
    
    /*
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
    
    GTBUserSingle *userSingle = [GTBUserSingle shareUserSingle];
    
    //  设置网页请求头
    if([userSingle isLogin])
    {
  
        [request addValue:userSingle.token forHTTPHeaderField:@"token"];
        [request addValue:userSingle.mobile forHTTPHeaderField:@"mobile"];
        
        NSString *version = [SJAppInformationTool gainApplicationVersion];
        
        [request addValue:version forHTTPHeaderField:@"version"];
        
    }
     
    
    //  加载网页
    [self.webView loadRequest:request];
     */
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
    
    //  加载网页
    [self.webView loadRequest:request];
    
}


/// 获取用户信息
- (void)getUserInfo
{

    [self requestDataInUserInfoWithSuccessBlock:^(id result) {
        
        NSString *parmString = [SJDataManageTool jsonClassConvertToJosnStringWithJsonClass:result];

        //  OC掉用JS方法        JS注入
        NSString *message = [NSString stringWithFormat:@"onNativeLoginSuccess(%@)",parmString];
        [self.webView evaluateJavaScript:message completionHandler:nil];
        
    }];
    
}



#pragma mark --------------响应方法--------------

//  点击关闭
- (void)closeItemClick:(UIBarButtonItem *)item
{
    //  关闭
    [self.navigationController popViewControllerAnimated:YES];
}

//  返回上一层
- (void)backButtonClick:(UIButton *)sender
{
    if (self.webView.canGoBack)
    {
        //  返回上一层
        [self.webView goBack];
        
    }else
    {
        [self.navigationController popViewControllerAnimated:YES];
        
    }
}


#pragma mark ----------WKWebView 代理方法----------


// 如果不添加这个，那么wkwebview跳转不了AppStore
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if ([webView.URL.absoluteString hasPrefix:@"https://itunes.apple.com"])
    {
        
        [[UIApplication sharedApplication]openURL:navigationAction.request.URL options:@{} completionHandler:nil];
        
        decisionHandler(WKNavigationActionPolicyCancel);
        
    }
    else
    {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
    
    
    //  网页跳转地址拦截
//    NSString *requestUrl = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    
    
 
//    if ([[requestUrl lowercaseString] isEqualToString:[@"gtb://APP_RECHARGE" lowercaseString]]) {
//
//        //  进入充值界面
//        [self enterRechargeVCWithMoney:nil];
//
//    }
    
}


// WKScriptMessageHandler       js掉用OC的方法
- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message
{
    
    
    if([message.name isEqualToString:@"login"])
    {
    }
    else if([message.name isEqualToString:@"getLoginUser"] || [message.name isEqualToString:@"userLogin"])
    {
        
        //  获取用户信息，用户登录     都进行登录+传递用户信息
        
        if(KUserSingeIsLogin)
        {
            
            //  已登录
            
            /// 获取用户信息
            [self getUserInfo];
            
        }
        else
        {
            
            [EBMLoginMethod naviagtionEnterLoginVCWithVC:self loginSuccessBlock:^{
                
               /// 获取用户信息
               [self getUserInfo];
                
            }];
            
        }
        
    }
    else if([message.name isEqualToString:@"callMPShare"])
    {
        
    }
    
}




@end
